// pages/usercenter/applyatuan/applyatuan.js
var utils = require('../../../utils/util');

Page({
  gotoPage: utils.gotoPage,

  /**
   * 页面的初始数据
   */
  data: {
    customItem: '全部',
    tuan: {
      fullname: '',
      mobile: '',
      wechat: '',
      region: '',
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var openid = wx.getStorageSync('openid');
    var HOST = getApp().globalData.HOST;
    var that = this;
  },

  inputChange: function(event) {
    var key = event.currentTarget.dataset.key;
    this.data.tuan[key] = event.detail.value;
    this.setData({
      tuan: this.data.tuan
    });
    console.log(this.data.tuan)
  },

  // 提交申请
  applyTuan: function(event) {
    var openid = wx.getStorageSync('openid');
    var HOST = getApp().globalData.HOST;
    var that = this;

    var fullname = that.data.tuan.fullname;
    var mobile = that.data.tuan.mobile;
    var wechat = that.data.tuan.wechat;
    var region = that.data.tuan.region;

    if (fullname == '' || mobile == '' || region == '') {
      wx.showToast({
        title: '请先填写申请信息',
        icon:'none'
      })
      return false;
    }

    wx.request({
      url: HOST + 'group',
      method: 'POST',
      data: {
        openid: openid,
        fullname: fullname,
        mobile: mobile,
        wechat: wechat,
        region: region.join(' '),
      },
      success: function(resp) {
        console.log(resp);
        wx.redirectTo({
          url: '/pages/usercenter/applyatuanok/applyatuanok',
        });
      },
    })
  },

  bindRegionChange: function(e) {
    this.setData({
      'tuan.region': e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    wx.showTabBar({});
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: '国学小书童',
      path: '/pages/index/index',
      imageUrl: '/images/g1.jpg',
    }
  }
})